package com.climber.android.commonres.util

import android.content.DialogInterface
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.climber.android.commonres.R
import io.ganguo.library.mvp.util.UIUtils
import io.reactivex.Observable

/**
 * Created by Mikes at 2019-06-27 11:09
 */
fun AlertDialog.setupDialogButtonColors() {
    val mButtonNegative: Button? = this.getButton(DialogInterface.BUTTON_NEGATIVE)
    mButtonNegative?.setTextColor(UIUtils.getColor(R.color.negative_button_color))
    val mButtonPositive: Button? = this.getButton(DialogInterface.BUTTON_POSITIVE)
    mButtonPositive?.setTextColor(UIUtils.getColor(R.color.positive_button_color))
}

fun AlertDialog.dialogToObservable(): Observable<AlertDialog> =
        Observable.create { emitter ->
            emitter.onNext(this@dialogToObservable)

            emitter.setCancellable {
                this@dialogToObservable.dismiss()
            }
        }

fun DialogFragment.dialogToObservable() : Observable<DialogFragment> =
    Observable.create { emitter ->
        emitter.onNext(this@dialogToObservable)

        emitter.setCancellable {
            this@dialogToObservable.dismiss()
        }
    }